<?php

namespace app\controllers;
use yii\base\ExitException;
use yii\db\Query;
use yii\web\Controller;


class ApiBaseController extends Controller{

    public $enableCsrfValidation = false;

    /**
     * 获取当前用户信息
     */
    protected function GetUser(){
        $method=\Yii::$app->request->method;
         if ($method=='GET'){
           $token=\Yii::$app->request->get();
           if(isset($token['token'])){
               $token=\Yii::$app->request->get()['token'];
           }else{
               throw new ExitException(json_encode(['code'=>-1,'msg'=>'缺少token值']));
           }
         }else if ($method=='POST'){
            $token=\Yii::$app->request->post();
             if(isset($token['token'])){
              $token=\Yii::$app->request->post()['token'];
             }else{
                 throw new ExitException(json_encode(['code'=>-1,'msg'=>'缺少token值']));
             }
         }
        $cache =\Yii::$app->cache;
        $user=$cache->get($token);
        if (!$user){
            throw new ExitException(json_encode(['code'=>-1,'msg'=>'找不到用户信息']));
        }
        return $user;
    }


    //获取企业招聘岗位
    public function GetWork($id){
        $list=(new Query())->select('id,name,on,city,region,experience,education,min_salary,max_salary,welfare,collection')->from('yii_work')->where(array('company'=>$id))->all();
        foreach ($list as $k=>$v){
            $list[$k]['city']=$this->GetRegion($v['city']);
            $list[$k]['region']=$this->GetRegion($v['region']);
            $list[$k]['experience']=$this->GetExperience($v['experience']);
            $list[$k]['education']=$this->GetEducation($v['education']);
            $list[$k]['welfare']=$this->GetWelfare($v['welfare']);
        }
        return $list;
    }


    //获取图片
    public function GetImg($id){
        $list=(new Query())->select('url')->from('yii_picture')->where(array('id'=>$id))->one();
        if ($list){
            return 'https://'.$_SERVER['HTTP_HOST'].$list['url'];
        }
    }

    //获取公司
    public function GetCompany($id){
        $list=(new Query())->select('name,logo')->from('yii_company')->where(array('id'=>$id))->one();
        if ($list){
            $list['logo']=$this->GetImg($list['logo']);
            return $list;
        }
    }

    //获取学历
    public function GetEducation($id){
        $list=(new Query())->select('name')->from('yii_education')->where(array('id'=>$id))->one();
        if ($list){
            return $list['name'];
        }
    }

    //获取经验
    public function GetExperience($id){
        $list=(new Query())->select('name')->from('yii_experience')->where(array('id'=>$id))->one();
        if ($list){
            return $list['name'];
        }
    }

    //企业人数
    public function GetNumber($id){
        $list=(new Query())->select('name')->from('yii_number')->where(array('id'=>$id))->one();
        return $list['name'];
    }

    //行业类别
    public function GetIndustry($id){
        $list=(new Query())->select('name')->from('yii_industry')->where(array('id'=>$id))->one();
        return $list['name'];
    }

    //获取融资情况
    public function GetFinancing($id){
        $list=(new Query())->select('name')->from('yii_financing')->where(array('id'=>$id))->one();
        if ($list){
            return $list['name'];
        }
    }

    //获取城市
    public function GetRegion($id){
        $list=(new Query())->select('name')->from('yii_region')->where(array('id'=>$id))->one();
        if ($list){
            return $list['name'];
        }
    }

    //获取企业环境图片
    public function GetCompanyImage($id){
        $list=(new Query())->select('img_id')->from('yii_company_image')->where(array('company_id'=>$id))->all();
        foreach ($list as $k=>$v){
            $list[$k]['img']=$this->GetImg($v['img_id']);
            unset($list[$k]['img_id']);
        }
        return $list;
    }


    //福利待遇
    public function GetWelfare($welfare){
      return explode('；',$welfare);
    }

    //获取Post请求ID
    public function PostId(){
        $id=(int)\Yii::$app->request->post()['id'];
        return $id;
    }

    //获取Get请求ID
    public function GetId(){
        $id=(int)\Yii::$app->request->get()['id'];
        return $id;
    }


}
